package cn.itsource.hrm.task;

import cn.itsource.hrm.domain.User;
import cn.itsource.hrm.service.IUserService;
import cn.itsource.hrm.utils.MyTools;
import cn.itsource.hrm.utils.excel.ReadExcelTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

//读取Excel入库操作
//@Component
public class ReadExcelTask {

    @Autowired
    private IUserService userService;

    //每X秒运行一次
    @Scheduled(cron = "0/58 * * * * *")//每20秒执行一次
    public void readExcelTask() {
        System.out.println("开始执行任务");
        //获取所有文件
        /**
         E:\excel\testExcel_124584.xlsx
         E:\excel\testExcel_124523.xlsx
         E:\excel\testExcel_124521.xlsx
         ...
         */
        List<String> excelPathList = MyTools.getFolderFile("E:\\excel");

        long t1 = System.currentTimeMillis();
        //TODO 主业务逻辑
        for(int i=0; i<excelPathList.size(); i++){
            String path = excelPathList.get(i);
            //读取该文件的内容
            List<User> userList = ReadExcelTools.readExcel(path);
            //入库
            userService.insertBatch(userList);
            //删除文件
            MyTools.deleteFile(path);
        }
        long t2 = System.currentTimeMillis();
        System.out.println("任务完成，耗时：" + (t2-t1) + "毫秒");//43763
    }
}